package com.hhs.storage.util;
 
import java.math.BigDecimal;
import java.util.Arrays;


/**
 *
 * Utility related to numerical value.
 * <PRE>
 * <B>Revision History : </B>
 *	<!-- ------------------------------------ START OF Revision History -->
 *	2005/11/07 New making
 *	<!-- ------------------------------------ END OF Revision History -->
 * </PRE>
 *
 */

public class NumberUtil {
   /**
    * Null is converted into "0". 
    * @param arg Character string
    * @return When it is null " 0"
    */
    public static String N2Z(String arg) {
        if (arg.equals("")) {
            arg = "0";
        }
        return arg;
    }
    /**
     * Null is converted into "0". 
     * @param arg Character string
     * @return When it is null " 0"
     */
     public static String N2ZZ(String arg) {
    	 arg=StringUtil.safeTrim(arg);
         if (arg.equals("")) {
             arg = "0";
         }
         return arg;
     }
   /**
    * It is checked whether it is a figure.
    * @param arg Character string
    * @return ture:It is a numerical value. false:It is not a numerical value.
    */
    public static boolean isNumber(String arg) {
        boolean flg = true;

        if (arg == null ) {
            return false;
        }

        if (arg.trim().equals("")){
            return false;
        }

        try {
            Double.parseDouble(arg);
        } catch (NumberFormatException e) {
                flg = false;
        }
        return flg;
    }

   /**
    * It is checked whether it is a minus figure.
    * @param arg  Character string
    * @return ture:It is a minus figure. false:It is not a minus figure.
    */
    public static boolean isMinus(String arg) {
        String num = new String();
        if (!(StringUtil.validString(arg))) {
            return false;
        }

        int start = 0;
        int end   = 0;

        if (arg.indexOf("△") > -1) {
            start = arg.indexOf("△") + "△".length();
            num = arg.substring(start);
        } else if (arg.indexOf("▲") > -1){
            start = arg.indexOf("▲") + "▲".length();
            num = arg.substring(start);
        } else if ((arg.indexOf("(") > -1) && (arg.indexOf(")") > -1)){
            start = arg.indexOf("(")+1;
            end = arg.indexOf(")")-1;

            num = arg.substring(start,end);
        }
        return isNumber(num);
    }

   /**
    * It converts it into the minus figure.
    * @param arg Character string
    * @return Minus numerical value(character string)
    */
    public static String toMinus(String arg) {
        String num = new String();
        if (!(StringUtil.validString(arg))) {
            return "0";
        }

        if (arg.indexOf("△") > -1) {
            num = "-" + arg.substring(arg.indexOf("△")+"△".length()).trim();
        } else if (arg.indexOf("▲") > -1){
            num = "-" + arg.substring(arg.indexOf("▲")+"▲".length()).trim();
        } else if ((arg.indexOf("(") > -1) && (arg.indexOf(")") > -1)){
            num = "-" + arg.substring(arg.indexOf("(")+1,arg.indexOf(")")).trim();
        }
        return num;
    }

   /**
    * It is checked whether it is a figure.
    * @param c One byte character
    * @return ture:It is a figure. false:It is not a figure.
    */
    public static boolean isNumber(char c) {
        if (c >= 0x30 && c <= 0x39 ){
            return true;
        } else {
            return false;
        }
    }

    /**
     * The character string is converted into the numerical value of the int type.
     * @param str Object character string
     * @return Numerical value of int type
     */
    public static int toIntCase(String str) {
        if (!StringUtil.validString(str)){
            return 0;
        }
        Integer num=new Integer(0);
        try{
        	num = new Integer(StringUtil.tozZero(str));
        }catch(NumberFormatException e){
        	Double dnum = new Double(StringUtil.tozZero(str));
        	return dnum.intValue();
        }
        return num.intValue();
    }

    /**
     * The character string is converted into the numerical value of the long type.
     * @param str Object character string
     * @return Numerical value of long type
     */
    public static long toLongCase(String str) {
        if (!StringUtil.validString(str)){
            return 0;
        }
        Long num=new Long(0);
        try{
        	num = new Long(StringUtil.tozZero(str));
        }catch(NumberFormatException e){
        	Double dnum = new Double(StringUtil.tozZero(str));
        	return dnum.longValue();
        }
        return num.longValue();
    }

    /**
     * new a BigDecimal with BigDecimal.value = num  
     * if num is null or num is not a number return a BigDecimal with BigDecimal.value =0 
     * BigDecimal.scale = scale
     * BigDecimal.roundingMode = BigDecimal.ROUND_DOWN
     * @param num value
     * @param scale count of scale
     * @param roundingMode mode of rounding
     * @return BigDecimal 
     * @author HuMing
     */
    public static BigDecimal str2BigDecimal(String num,int scale,int roundingMode) {
        BigDecimal ret = null;
        if (num==null || !isNumber(num)){
            ret = new BigDecimal("0");
        } else {
            ret = new BigDecimal(num);
        }
        ret = ret.setScale(scale,roundingMode);
        return ret;
    }
    /**
     * new a BigDecimal with roundingMode = BigDecimal.ROUND_DOWN
     * @see NumberUtil#str2BigDecimal(String, int, int)   
     * @param num value
     * @param scale count of scale
     * @return BigDecimal 
     * @author HuMing
     */
    public static BigDecimal str2BigDecimalDown(String num,int scale) {
        return str2BigDecimal(num,scale,BigDecimal.ROUND_DOWN);
    }
    
    /**
     * 字符串变成小数可以定精度 
     * @param num
     * @param scale
     * @return
     */
    public static double toDoubleCase(String num,int scale) {
        return str2BigDecimal(num,scale,BigDecimal.ROUND_DOWN).doubleValue();
    }
    
    /**
     * 字符串变成小数多少万元
     * 保留两位小数
     * @param num
     * @return
     */
    public static String toDoubleCaseWan(String num) {
    	String dis="";
    	BigDecimal bd=str2BigDecimal(num,2,BigDecimal.ROUND_DOWN);
    	if(bd.compareTo(new BigDecimal("100000"))>0){
    		bd=bd.divide(new BigDecimal("10000"));
    		bd=str2BigDecimal(bd+"",2,BigDecimal.ROUND_DOWN);
    		dis=bd+"万";
    	}else{
    		bd=str2BigDecimal(num,0,BigDecimal.ROUND_DOWN);
    		dis=bd+"";
    	}
        return dis;
    }
    /**
     * 字符串变成小数精度为2
     * @param num
     * @return
     */
    public static double toDoubleCase(String num) {
        return str2BigDecimal(num,2,BigDecimal.ROUND_DOWN).doubleValue();
    }
    /**
     * process round
     * @param num the number to be round
     * @param scale count of scale
     * @return result
     * @author Luoyue
     */
    public static String round(double num,int scale){
        if(scale<0){
          return String.valueOf(num);
        }
        BigDecimal b = new BigDecimal(Double.toString(num));
        return b.setScale(scale,BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 整形数据均分方法
     * @param num the number to be round
     * @return 整形数
     * @author 均分份数
     */
    public static long[] longavg(long num,int fs){
        long[] ln=new long[fs];
        double avgd=NumberUtil.toDoubleCase(((double)num)/fs+"",0);
        long summ=0;
        for (int i = 0; i < ln.length-1; i++) {
			ln[i]=(long)avgd;
			summ+=avgd;
		}
        ln[ln.length-1]=num-summ;
        return ln;
    }
}
